//import all the GUI classes
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import coreAuction.Auction;


/**
 * Simple GUI for Auction application
 */
public class AuctionGUI extends JFrame 
{

    private Auction auction;
    
    //GUI components
    JTextArea display;
    JTextField searchField;
    JButton processButton, showCustButton, showBidsButton, showItemsButton;
    JTextField processMessage;
    JPanel centrePanel;
    
    /**
     * Create the frame with its panels.
     */
    public AuctionGUI(Auction auction)
    {
        this.auction = auction;
        
        //set up window title
        setTitle("Auction");
        //ensure program ends when window closes
		setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(100,500);
        
        //add panel and result field to the content pane
        Container contentPane = getContentPane();
        contentPane.add(createNorthPanel(), BorderLayout.NORTH);
        contentPane.add(createCentrePanel(),BorderLayout.CENTER);
        contentPane.add(createEastPanel(), BorderLayout.EAST);
        
        //pack and set visible
        pack();
        setVisible(true);
    }
    
    private JPanel createEastPanel() {

    	showCustButton = new JButton("Customers");
    	showCustButton.addActionListener( new ShowCustsController());
    	showItemsButton = new JButton("Items");
    	showItemsButton.addActionListener( new ShowItemsController());
    	showBidsButton = new JButton("bids");
    	showBidsButton.addActionListener( new ShowBidsController());
    	showBidsButton.setEnabled(false);
    	
    	JPanel eastPanel = new JPanel(new GridLayout (3,1));
    	eastPanel.setPreferredSize(new Dimension(100,0));   	

    	eastPanel.add(showCustButton);
    	eastPanel.add(showItemsButton);
    	eastPanel.add(showBidsButton);
    	return eastPanel;
    }
    private JPanel createNorthPanel() {
        //north panel shows the button to start processing

        processButton = new JButton("Process All Bids");
        processButton.addActionListener( new ProcessBidsController());
        processMessage = new JTextField(30);
        processMessage.setEditable(false);
        processMessage.setText("Processing has not started yet");
        JPanel northPanel = new JPanel();
        northPanel.add(processButton);
        northPanel.add(processMessage); 
        return northPanel;
    }
    
    private JPanel createCentrePanel() {
    	centrePanel = new JPanel();
        //Set up the area where the results will be displayed.
        display= new JTextArea(20,70);     
        display.setEditable(false);
        Font df = new Font(Font.MONOSPACED, Font.PLAIN, 12);
        display.setFont(df);
        //initially set to Items
        display.setText("LIST OF ITEMS\n");
        display.append(auction.getItemReport()); 
        centrePanel.add(display);
        return centrePanel;
    }
    

    private class ShowItemsController  implements ActionListener
    {
        public void actionPerformed(ActionEvent e) 
        { 
        	display.setText(auction.getItemReport());
        }
    }
    
    private class ShowBidsController  implements ActionListener
    {
        public void actionPerformed(ActionEvent e) 
        { 
        	display.setText(auction.getBidReport());
        }
    }
    
    private class ProcessBidsController  implements ActionListener
    {
        public void actionPerformed(ActionEvent e) 
        { 
    		auction.processBids();
    		showBidsButton.setEnabled(true);
    		processButton.setEnabled(false);
    		processMessage.setText("Processing finished");
    		display.setText("LIST OF ITEMS AFTER AUCTION\n");
            display.append(auction.getItemReport());

        }
    }
    
    private class ShowCustsController  implements ActionListener
    {
        public void actionPerformed(ActionEvent e) 
        { 
        	display.setText(auction.getCustList());
        }
    }


}
